
var scanShipObj = {
    /**
     * 描述 : 输入快递单号
     * 作者 : Zoro.Zhu
     */
    "getSE": function (event) {
        event = event ? event : window.event;
        if (event.keyCode == 13) {
            var singleExpress = $('#singleExpress').val();
            if (singleExpress === '') {
                return false;
            }
            $('#falseRemark').text('');
            $('#trueRemark').html('');
            $.ajax({
                url: ROOT_URL + "/?c=ctrl_wish_scanShip&a=getOrderInfo",
                type: "POST",
                cache: false,
                async: false,
                data: {singleExpress:singleExpress},
                dataType: "json",
                success: function (data) {
                    if(data.state !== 200){
                        $('#falseRemark').text(data.info);
                    } else {
                        var html = "<table style='width: 90%'>";
                        var info = data.data;
                        for (var i in info) {
                            html += "<tr><td><span style='font-size: 25px'>包裹号：</span><span style='font-size: 22px'>" + info[i].parcelNum + "</span></td>" +
                                "<td><span style='font-size: 25px'>归属人：</span><span style='font-size: 22px'>" + info[i].user + "</span></td>" +
                                "<td><span style='font-size: 25px'>物流渠道：</span><span style='font-size: 22px'>" + info[i].logistics + "</span></td>" +
                                "<td><span style='font-size: 25px'>跟踪号：</span><span style='font-size: 22px'>" + info[i].trackNo + "</span></td>";
                            if(info[i].trackNo !== ''){
                                html += "<td><input value='打印面单' class=\"btn btn-warning\" onclick=\"scanShipObj.printOk('" + info[i].dxmAccount + "','" + info[i].orderId + "','" + info[i].logisticsId + "','" + info[i].parcelNum + "')\"></td>";
                            } else {
                                html += "<td><span style='font-size: 25px;color: #FF0000'>没交运，无法打印</span></td>";
                            }
                            html += "</tr>";
                            var product = info[i].productInfo;
                            html += "<tr><td colspan='5' width='100%'><span style='font-size: 25px'>产品信息：</span><table width='80%' style='text-align: center'>";
                            for(var p in product){
                                html += "<tr>" +
                                    "<td><img src='" + product[p].img + "' style='width: 120px;height: 120px'></td>" +
                                    "<td><span style='font-size: 25px'>SKU：</span><span style='font-size: 22px'>" + product[p].sku + "</span></td>" +
                                    "<td><span style='font-size: 25px'>数量：</span><span style='font-size: 22px;color: #0000FF'>" + product[p].qty + "</span></td>" +
                                    "<td><span style='font-size: 25px'>付款时间：</span><span style='font-size: 22px'>" + product[p].paymentTime + "</span></td>" +
                                    "<td><span style='font-size: 25px'>金额：</span><span style='font-size: 22px'>" + product[p].currency + ' ' + product[p].priceTotal + "</span></td>" +
                                    "</tr>";
                            }
                            html += "</table></td></tr>";
                        }
                        $('#trueRemark').html(html);
                    }
                }
            });
        }
    },

    /**
     * 描述 : 标记打印完成
     * 作者 : Zoro.Zhu
     */
    'printOk' : function (account,orderId,logisticsId,parcelNum) {
        var autoPrint = $('input:radio[name="autoPrint"]:checked').val();
        $.ajax({
            url: ROOT_URL + "/?c=ctrl_wish_dxmOrder&a=printOk",
            type: "POST",
            data: {account:account,orderId:orderId,logisticsId:logisticsId,parcelNum:parcelNum},
            success: function (result) {
                result = JSON.parse(result);
                if (result.code !== 200) {
                    layer.alert(result.info);
                    return false;
                } else {
                    if(autoPrint == 1){
                        pdfjsLib.GlobalWorkerOptions.workerSrc = ROOT_URL + '/view/js/pdfjs/build/pdf.worker.js';
                        var pdfData = atob(result.pdf);
                        var CMAP_URL = ROOT_URL + '/view/js/pdfjs/web/cmaps';
                        var loadingTask = pdfjsLib.getDocument({
                            data: pdfData,
                            cMapUrl: CMAP_URL,
                            cMapPacked: true,
                        });
                        // var loadingTask = pdfjsLib.getDocument({ url: result.url, });//  _url为后台请求pdf流的路径
                        loadingTask.promise.then(function(pdf) {
                            //此处适用pdf只有一个page情况
                            pdf.getPage(1).then(function(page) {
                                //scale调节生成的画布图片大小，与清晰度有关，需要调整。
                                var scale = 2;
                                var viewport = page.getViewport(scale);
                                //将生成的page对象加载到canvas中
                                var canvas = document.getElementById('thepdfcanvas2');
                                var context = canvas.getContext('2d');
                                canvas.height = viewport.height;
                                canvas.width = viewport.width;
                                canvas.style.width = "100%";
                                canvas.style.height = "100%";
                                var renderContext = {
                                    canvasContext: context,
                                    viewport: viewport
                                };
                                //此处开始画画布
                                page.render(renderContext).promise.then(function () {
                                    var LODOP;
                                    LODOP = getLodop();
                                    LODOP.PRINT_INIT("面单标签");
                                    LODOP.SET_PRINT_PAGESIZE(2, 1000, 1000, "User defined");
                                    LODOP.SET_PRINT_MODE("FULL_WIDTH_FOR_OVERFLOW", true);
                                    LODOP.SET_PRINT_MODE("FULL_HEIGHT_FOR_OVERFLOW", true);
                                    //<canvas> 元素转换为.png图片的BASE64编码
                                    var canvasEle = document.getElementById("thepdfcanvas2");
                                    var dataUrl = canvasEle.toDataURL();
                                    var html = '<img style="width: 910px;height: 900px;" src="' + dataUrl + '">';
                                    LODOP.ADD_PRINT_HTM('0%','0%','100%','100%',html);
                                    LODOP.PRINT();
                                });

                            });
                        });
                    } else {
                        window.open("https://www.dianxiaomi.com/printMb.htm?packageIds=" + orderId + "&authId=" + logisticsId + "&isCos=1","_blank");
                    }
                    return false;
                }
            }
        });
    },
};

//初始化聚焦
$('#singleExpress').focus();